package com.ecourseapp.ActivityPool;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.ecourseapp.MainActivity;
import com.ecourseapp.NativeForRN.VideoPlayerWithControllerActivity;
import com.ecourseapp.R;
import com.ecourseapp.util.ECourseConstant;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.security.MessageDigest;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ActivityPageOne extends AppCompatActivity {

    public static String loginJson = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_native_page_one);
    }

    public void onClickToHomePage(View view) {
        Intent intent = new Intent(ActivityPageOne.this, MainActivity.class);
        ECourseConstant.eCourseFirstRouter = "HomePage";
        startActivity(intent);
    }


    public void onClickToPage(View view) {
        EditText etPageName = (EditText) findViewById(R.id.et_page_name);
        EditText etCourseOrLessonId = (EditText) findViewById(R.id.et_course_or_lesson_id);

        Intent intent = new Intent(ActivityPageOne.this, MainActivity.class);

        ECourseConstant.eCourseFirstRouter = etPageName.getText().toString();
        if (ECourseConstant.eCourseFirstRouter.equals("DetailLesson")) {
            ECourseConstant.map.put("entryCourseId", etCourseOrLessonId.getText().toString());
        } else {
            ECourseConstant.map.put("entryLessonId", etCourseOrLessonId.getText().toString());
        }
        startActivity(intent);
    }

    public void login(View view) {
        EditText etUserName = (EditText) findViewById(R.id.et_username);
        EditText etPassword = (EditText) findViewById(R.id.et_password);
        final JSONObject object = new JSONObject();
        try {
            object.put("username", etUserName.getText().toString());
            object.put("password", encryptSHA256(etPassword.getText().toString()));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        loginJson = object.toString();
        OkGo.<String>post("http://test.family.juziwl.cn/family/user/register/login")
                .upJson(loginJson)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<String> response) {
                        LoginResponse loginResponse = new Gson().fromJson(response.body(), LoginResponse.class);
                        if (loginResponse.status == 200) {
                            ECourseConstant.userInfo.loginType = loginResponse.content.userType;
                            ECourseConstant.userInfo.uuid = loginResponse.content.userId;
                            ECourseConstant.userInfo.token = loginResponse.content.token;
                            ECourseConstant.userInfo.imId = loginResponse.content.imId;
                            Toast.makeText(getApplicationContext(), "Login Success", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getApplicationContext(), loginResponse.errorMsg, Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    public static String encryptSHA256(String content) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(content.getBytes());
            return bytesToHexString(md.digest());
        } catch (Exception var2) {
            var2.printStackTrace();
            return "";
        }
    }

    public static String bytesToHexString(byte[] src) {
        if (src != null && src.length > 0) {
            StringBuilder stringBuilder = new StringBuilder("");

            for (int i = 0; i < src.length; ++i) {
                int v = src[i] & 255;
                String hv = Integer.toHexString(v);
                if (hv.length() < 2) {
                    stringBuilder.append(0);
                }

                stringBuilder.append(hv);
            }

            return stringBuilder.toString();
        } else {
            return "";
        }
    }

    public void watchVideo(View view) {
        VideoPlayerWithControllerActivity.naveToActivity(this,
                "http://dfs.video.jzexueyun.com/exue/20180821/bfae74c8a8c9484baa559add91aa3e31.mp4");
    }
}
